package com.sicheng.algorithm.recursive;

/**
 * @author zsc
 * @version 1.0
 * @date 2021/8/28 14:21
 */
public class InsertValueSearch {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println(insertValueSearch(arr, 0, arr.length - 1, 10));
    }

    public static int insertValueSearch(int[] arr, int start, int end, int key) {
        if (start > end || key < arr[0] || key > arr[arr.length - 1]) {
            return -1;
        }
        int mid;
        if (arr[start] == arr[end])
            mid = arr[start];
        else mid = start + (end - start) * (key - arr[start]) / (arr[end] - arr[start]);
        if (key > arr[mid])
            return insertValueSearch(arr, mid + 1, end, key);
        if (key < arr[mid])
            return insertValueSearch(arr, start, mid - 1, key);
        else return mid;
    }
}
